package com.fang.gateway.security.utils;

import com.alibaba.fastjson.JSONObject;
import com.fang.common.common.CommonConstants;
import com.fang.common.common.CommonResult;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.io.UnsupportedEncodingException;

/**
 * @Description Handle返回公共封装
 * @Author Bernie_fang
 * @Date 2025/3/9 15:07
 **/
public class HandlerUtils {

    public static Mono<Void> writeWith(ServerWebExchange exchange, CommonResult result){
        ServerHttpResponse response = exchange.getResponse();
        String body = JSONObject.toJSONString(result);
        DataBuffer buffer = null;
        try{
            buffer = response.bufferFactory().wrap(body.getBytes(CommonConstants.CommonResponse.UTF8));
        }catch(UnsupportedEncodingException ue){
            ue.printStackTrace();
        }
        return response.writeWith(Mono.just(buffer));
    }

}
